package com.laxcen.basiclib.core.updateH5;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;

import com.laxcen.basiclib.R;
import com.laxcen.basiclib.base.BaseDialog;
import com.laxcen.basiclib.core.task.TaskManager;
import com.laxcen.basiclib.core.update.UpdatePush;
import com.laxcen.basiclib.core.update.widget.NumberProgressBar;
import com.laxcen.basiclib.core.updateH5.task.H5UpdateTask;
import com.laxcen.basiclib.utils.LogUtil;

/**
 *  H5 更新进度条
 */
public class H5UpdateDialog extends BaseDialog{
    public H5UpdateDialog(Context context) {
        super(context, R.layout.update_dialog_h5);
        hideCancl();
        hideSubmit();
        setOutsideTouchable(false);
        npb = view.findViewById(R.id.npb_progress);
        npb.setMax(1000);
    }

    private NumberProgressBar npb;
    @Override
    public void onClose() {

    }

    @Override
    public boolean onCancel() {
        return false;
    }

    @Override
    public boolean onSubmit() {
        return false;
    }

    private String path;

    public void setPath(String path) {
        this.path = path;
    }

    public void onShow(){
        show(0, Gravity.CENTER);
        H5UpdateTask h5UpdateTask = new H5UpdateTask();
        h5UpdateTask.set("callback", new H5UpdateTask.Callback() {
            @Override
            public void onProgress(int i) {
                LogUtil.e("onProgressOn",i);
                npb.setProgress(i);
            }

            @Override
            public void onResult(boolean isOk,String msg) {
                if(isOk){
                    setTitle("更新完成。即将打开...");
                    TaskManager.getInstance().exec(() -> {
                        dismiss();
                        UpdatePush.updateResult(true,isOk);
                    },2000);
                }else{
                    setTitle("更新失败:"+msg);
                }
            }
        });
        h5UpdateTask.set("path",path);
        TaskManager.getInstance().exec(h5UpdateTask);
    }
    public static void update(Activity activity, String path){
        H5UpdateDialog h5UpdateDialog = new H5UpdateDialog(activity);
        h5UpdateDialog.setTitle("安装中...");
        h5UpdateDialog.setPath(path);
        h5UpdateDialog.onShow();
    }

}
